﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Collections.Generic;
using System.ServiceModel;


namespace MissedMemo.MeatAndCheeseDisc.Services
{
    //[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)] (not needed if comms are all OneWay)
    public class OrderEntryManagementService : IOrderEntryManagementService
    {
        private static List<IOrderEntryManagerNotification> _subscribers = new List<IOrderEntryManagerNotification>();


        public void SubscribeOrderEntryManagerNotifications()
        {
            IOrderEntryManagerNotification subscriber = OperationContext.Current.GetCallbackChannel<IOrderEntryManagerNotification>();

            _subscribers.Add( subscriber );
        }


        public void UnsubscribeOrderEntryManagerNotifications()
        {
            IOrderEntryManagerNotification subscriber = OperationContext.Current.GetCallbackChannel<IOrderEntryManagerNotification>();

            _subscribers.Remove( subscriber );
        }
        
        
        // Extract pizzas from order, and send to kitchen
        // (Orders currently contain ONLY pizzas, but this will change)

        public void ProcessOrder( Order order )
        {
            foreach( object item in order.Items )
            {
                if( item is Pizza )
                {
                    KitchenOrderMonitorService.ProcessNewOrder( order.NowServingNumber, item as Pizza );
                }
            } 
        }


        // Relay cancellation notifications to kitchen
        // (currently using NowServingNumber as order ID, but these should be separate)

        public void DeleteOrder( int orderID )
        {
            KitchenOrderMonitorService.CancelOrder( orderID );
        }


        // Relay notification from kitchen to all (probably one) order entry clients, that a specific pizza is done
        
        internal static void NotifyPizzaIsDone( int nowServingNumber, Pizza pizza )
        {
            foreach( IOrderEntryManagerNotification subscriber in _subscribers )
            {
                subscriber.OnNotificationPizzaIsDone( nowServingNumber, pizza );
            }
        }
    }
}
